<?php

/**
 * --------------------------------
 * Default StudentSubjectScoreController
 *
 * 学生成绩管理的统一类  StudentSubjectScoreController
 * 
 * --------------------------------
 */

class StudentSubjectScoreController extends BaseController{

	// /**
 //     * 构造函数
 //     * 统一用户Session验证
 //     * 
 //     */
 //    public function __construct(){
 //    	$check_result = Basecontroller::session('internal');
 //    	if(!$check_result) 
 //    		exit();
 //    }
	/**
	 * 查询所有教师信息接口 
	 * @param
	 * @return Response
	 */
	public function index(){
     
		$teacher_result = DB::table('teacher')
		                    ->select('id','name','nativeid','sex','tel','birthday','introduction')
		                    ->paginate(15)
		                    ->toJson();
		$teacher_result = json_decode($teacher_result);                    
		return Response::json(['error'=>false,'result'=>$teacher_result],200);
    }

    /**
	 * 查询单个教师信息接口 
	 * @param  $id
	 * @return Response
	 */
    public function show($id){
      
    	$teacher_result = DB::table('teacher')
    	                    ->select('id','name','nativeid','sex','tel','birthday','introduction')
    	                    ->where('id',$id)
    	                    ->get();
    	if(!$teacher_result){
    		return Response::json(['error'=>true,'message'=>'记录不存在'],201);
    	}else{                   
    		return Response::json(['error'=>false,'result'=>$teacher_result],200);
    	} 
    }

    /**
	 * 新增教师接口 
	 * @param 
	 * @return Response
	 */
	public function store(){

		$search_result=DB::select("DESCRIBE teacher");
		foreach ($search_result as $value){
		    $fields[] = $value->Field;
        }

        if(!Input::has("name") || !Input::has("nativeid") ){
	 		return Response::json(['error'=>true,'message'=>"参数错误"], 201);
	 	}
	 	if(strlen(Input::get("nativeid")) <6){
	 		return Response::json(['error'=>true,'message'=>"身份证号过短"], 201);
	 	}

	 	$insert_arr = array();

	 	foreach ($fields as $field_name) {
	 	 	if(Input::has($field_name) && $field_name!="id"){
	 	 		$insert_arr[$field_name] = Input::get($field_name);
	 	 	}
	 	}

	 	$salt_arr = [1,2,3,4,5,6,7,8,9,0,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
	 	$salt = $salt_arr[rand(0,35)].$salt_arr[rand(0,35)].$salt_arr[rand(0,35)].$salt_arr[rand(0,35)];
	 	$password = substr(Input::get("nativeid"),-6);
	 	$insert_arr['salt'] 	= $salt;
	 	$insert_arr['password'] = md5($salt.$password);
	 	$insert_arr['teacher_role_id']  = "2";

	 	$id = DB::table('teacher')->insertGetId($insert_arr);
	 	if($id){
	 		return Response::json(['error'=>false,'message'=>'创建成功','result'=>$id],200);	
	 	}else{
	 		return Response::json(['error'=>true,'message'=>'创建失败'],201);
	 	}
		 	
	}
    
     /**
	  * 修改教师信息接口 
	  * @param 
	  * @return Response
	  */
	 public function update($id){
	 
	 	$input_param = Input::all();
	 	
	 	$relevant_param = array('id','name','nativeid','sex','tel','birthday','introduction');

	 	$teacher_update_arr = array();

	 	foreach ($input_param as $key => $val){
	 		if(in_array($key, $relevant_param)){
	 			$teacher_update_arr[$key] = $val;
	 		}
	 	}

	 	try{
	 		DB::table('teacher')->where('id','=',$id)->update($teacher_update_arr);
	 		return Response::json(['error'=>false,'message'=>'修改成功'],200);
	 	}catch(Exception $e){
			return Response::json(['error'=>true,'message'=>'修改失败'], 201);
		}
	 }

	

	/**
	 * 删除教师接口 
	 * @param 
	 * @return Response
	 */
	public function destroy($id){

	    $del_result = DB::table('ed_teacher')->where('id',$id)->delete();
	    if($del_result){
	    	return Response::json(['error'=>false,'message'=>'删除成功'],200);
	    }else{
	    	return Response::json(['error'=>true,'message'=>'删除失败'],201);
	    }

	}

	/**
	 * 教师搜索接口 
	 * @param 
	 * @return Response
	 */
	public function search(){

		$column = Input::get('column');
        $content = Input::get('content');
		if(!$column || !$content){
			return Response::json(['error'=>true,'message'=>'缺少重要参数'],201);
		}

		$search_result = DB::table('ed_teacher')
		                   ->select('*');

		if( $column == 'name' ){
			$search_result = $search_result->where('name','like','%'.$content.'%');
		}

		if($column == 'sex'){
			$search_result = $search_result->where('sex','like','%'.$content.'%');
		}

		if($column == 'tel'){
			$search_result = $search_result->where('ed_teacher.name','like','%'.$content.'%');
		}

		if($column == 'is_married'){
			$search_result = $search_result->where('ed_teacher.tel','like','%'.$content.'%');
		}

		if($column =='social_relationship'){
			$search_result = $search_result->where('ed_classed.student_num','like','%'.$content.'%');
		}
		if($column =='teach_age'){
			$search_result = $search_result->where('ed_classed.student_max','like','%'.$content.'%');
		}
		if($column =='birthday'){
			$search_result = $search_result->where('ed_classed.start_date','like','%'.$content.'%');
		}
		if($column =='edu'){
			$search_result = $search_result->where('ed_classed.lesson_num','like','%'.$content.'%');
		}
		if($column =='major'){
			$search_result = $search_result->where('ed_classed.duration','like','%'.$content.'%');
		}
		if($column =='residence_place'){
			$search_result = $search_result->where('ed_classed.lesson_tuition','like','%'.$content.'%');
		}
		if($column =='address'){
			$search_result = $search_result->where('ed_classed.tuition','like','%'.$content.'%');
		}

		$search_result = $search_result->paginate(15)->toJson();
		$result = json_decode($search_result);                  
		if(empty($result->data)){
			return Response::json(['error'=>true,'message'=>'教师不存在'],201);
		}else{
			return Response::json(['error'=>false,'result'=>$result],200);
		}


	}

    /**
	 * 教师所教班级搜索接口 
	 * @param  $id
	 * @return Response
	 */
    public function classes($id){

    	$classes_result = DB::table('ed_classes')
    	                    ->join('ed_teacher','ed_classes.teacher_id','=','ed_teacher.id')
    	                    ->join('ed_school','ed_classes.school_id','=','ed_school.id')
    	                    ->select('ed_classes.*','ed_teacher.name as teacher_name','ed_school.name as school_name')
    	                    ->where('ed_teacher.id',$id)
    	                    ->get();
    	if(!$classes_result){
    		return Response::json(['error'=>true,'message'=>'记录不存在'],201);
    	}else{
    		return Response::json(['error'=>false,'result'=>$classes_result],200);
    	}                    
    } 
}